package com.cwt_taxi_xa.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class DecimalUtil {

	/**
	 * 价格格式化
	 * @param price
	 * @return
	 */
	public static String priceFormat(double price){
		DecimalFormat df = new DecimalFormat("######0.00");
		return df.format(price);
	}
	/**
	 * 经纬度格式化
	 * @param price
	 * @return
	 */
	public static String gpsFormat(double price){
		DecimalFormat df = new DecimalFormat("######0.000000");
		return df.format(price);
	}

	/**
	 * 价格格式化
	 * @param price
	 * @return
	 */
	public static String priceFormat(String price){
		double amount = Double.parseDouble(price);
		return priceFormat(amount);
	}

	/**
	 * 元转分
	 * @param price
	 * @return
	 */
	public static String y2f(double price){
		return priceFormat2(price*100);
	}

	/**
	 * 元转分
	 * @param price
	 * @return
	 */
	public static String y2f(String price){
		double priceD = Double.parseDouble(price);
		return priceFormat2(priceD*100);
	}

	/**
	 * 价格格式化
	 * @param price
	 * @return
	 */
	private static String priceFormat2(double price){
		DecimalFormat df = new DecimalFormat("######0");
		return df.format(price);
	}

	public static double f2yDouble(int amount){
		BigDecimal decimal = new BigDecimal(amount);
		decimal = decimal.divide(new BigDecimal(100));
		double yuan = decimal.doubleValue();
		return yuan;
	}

	public static String f2yString(int amount){
		String amountStr = priceFormat(f2yDouble(amount));
		amountStr = amountStr.replace(".00", "");
		return amountStr;
	}

	/**
	 * obd数据中double值转int（保留一位小数并转int）
	 * 5.88转成58
	 * @param value
	 * @return
	 */
	public static int double2Int(String value){
		// String 转 double
		double oneDouble = Double.parseDouble(value);
		// double乘于10
		BigDecimal decimal = new BigDecimal(oneDouble);
		decimal = decimal.multiply(new BigDecimal(10));
		double newValue = decimal.doubleValue();
		return (int)newValue;

	}
}
